package org.viki.ljspider;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayDeque;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;





public class DbHelper {
	private static final Logger LOGGER = LoggerFactory.getLogger(DbHelper.class);
    public static final String DB_URL = "jdbc:mysql://127.0.0.1/LJHouseData";  
    public static final String DB_DRIVER = "com.mysql.jdbc.Driver";  
    public static final String DB_USER = "admin";  
    public static final String DB_PASSWORD = "admin"; 
    
    public static Connection conn = null;  
    private DbHelper(){
    	getConn();
    }  
    public static Connection getConn(){
    	if(conn == null){
    		try {
    			 Class.forName(DB_DRIVER);//指定连接类型  
			     conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);//获取连接  
			     conn.setAutoCommit(false);  //设为手动提交
			} catch (Exception e) { 
				e.printStackTrace();
			}
    	}
    	return conn;
    }
    
    public static ArrayDeque<String> getLocation(){
    	String sql = "SELECT * FROM location";
    	ArrayDeque<String> list = new ArrayDeque<String>();
    	try {
    		PreparedStatement pst = getConn().prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				list.add(rs.getString("value"));
			}
			rs.close();
			pst.close();
			
		} catch (SQLException e) {
			LOGGER.error("行政区读取失败！",e);
		}
    	return list;
    }
}
